 package com.baidu.common.async_http;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


public class ThreadPool
{
	private static ThreadPool instance;
	
	public synchronized static ThreadPool getInstance()
	{
		if(instance==null)
			instance=new ThreadPool();
		
		return instance;
	}
	
	private ThreadPoolExecutor mDownloadThreadPool = null;

	public ThreadPool()
	{
		// 初始化线程池
		mDownloadThreadPool = new ThreadPoolExecutor(0, Integer.MAX_VALUE,  200L, TimeUnit.SECONDS,  
                new SynchronousQueue<Runnable>());
		
		mDownloadThreadPool.setThreadFactory(new ThreadFactory()
		{
			@Override
			public Thread newThread(Runnable runnable)
			{
				Thread t = new Thread(runnable);
				t.setPriority(Thread.NORM_PRIORITY);
				return t;
			}

		});
	}
	
	public void execute(Runnable task)
	{
		mDownloadThreadPool.execute(task);		
	}
	
	public void shutdown()
	{
		mDownloadThreadPool.shutdown();
	}

}
